/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Negocio;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author Usuario
 */
public class Validaciones
{
    
    public boolean ValidarCedula(String cadena) 
    {
       
        int num_provincias = 24;

        if (cadena.length()==10)
        {
            try
            {
        
                int prov = Integer.parseInt(cadena.substring(0, 2));

                if (!((prov > 0) && (prov <= num_provincias))) 
                {
                    //JOptionPane.showMessageDialog(null, "No es Cedula Correcta");
                    return false;
                }
                int[] d = new int[10];
                for (int i = 0; i < d.length; i++) {
                    d[i] = Integer.parseInt(cadena.charAt(i) + "");
                }
                int imp = 0;
                int par = 0;
                for (int i = 0; i < d.length; i += 2) {
                    d[i] = ((d[i] * 2) > 9) ? ((d[i] * 2) - 9) : (d[i] * 2);
                    imp += d[i];
                }
                for (int i = 1; i < (d.length - 1); i += 2) {
                    par += d[i];
                }

                int suma = imp + par;

                int d10 = Integer.parseInt(String.valueOf(suma + 10).substring(0, 1) +
                        "0") - suma;

                d10 = (d10 == 10) ? 0 : d10;

                if (d10 == d[9]) 
                {
                    //JOptionPane.showMessageDialog(null, "Cedula Correcta");
                    return true;
                }
                else 
                {
                    return false;
                    //JOptionPane.showMessageDialog(null, "No es Cedula Correcta");
                }
            }
            catch( NumberFormatException ex)
            {
                return false;
                //JOptionPane.showMessageDialog(null, "La Cadena debe ser Numerica");
            }
        }
        else
        {
           return false;
           // JOptionPane.showMessageDialog(null, "La Cadena no es Cedula Correcta");
        }
    }
    
    public boolean ValidarCorreoElectronico(String cadena) {
        Pattern pat = null;
        Matcher mat = null;
        pat = Pattern.compile("^([0-9a-zA-Z]([_.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+([a-zA-Z]{2,9}.)+[a-zA-Z]{2,3})$");
        mat = pat.matcher(cadena);
        if (mat.find()) {
            //JOptionPane.showMessageDialog(null, "Es un Correo Electronico");
            return true;
        }
        else
        {
            //JOptionPane.showMessageDialog(null, "No es un Correo Electronico");
            return false;
        } 
    }
    
    public boolean ValidarNumeros(String caden) {
       try
       {
            Integer.parseInt(caden);
            //JOptionPane.showMessageDialog(null, "Es Numero");
            return true;
       } 
       catch (NumberFormatException nfe)
       {
            //JOptionPane.showMessageDialog(null, "No es Numero");
           return false;
       }
    }
    
    public boolean ValidarTexto(String cadena) 
    {
        
        int bandera=0; 
        for(int i = 0; i < cadena.length(); i++)
        {
        if(!((cadena.charAt(i) > 64 && cadena.charAt(i) < 91) ||(cadena.charAt(i) > 96 && cadena.charAt(i) < 123) || cadena.charAt(i)==32 ))
            bandera++;    
        }
        if (bandera==0)
        {
            return true;
        }
        else
        {
            return false;
        }

    }
    
    
    
}
